package com.wzs.service.impl;

import com.wzs.bean.User;
import com.wzs.bean.UserLoginLog;
import com.wzs.repository.impl.UserRepository;
import com.wzs.service.UserService;
import com.wzs.utils.BGUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository repository;

    /**
     * 新增用户
     * @param user
     * @return
     */
    public Integer addUser(User user) {
        user.setPassword(BGUtils.MD5(user.getPassword()));
        user.setCreateTime(BGUtils.GetTime());
        user.setState("1");
        Integer save = repository.save(user);
        return save;
    }

    /**
     * 登录
     * @param user
     * @return
     */
    public User login(User user) {
        if(StringUtils.isEmpty(user.getUserName()) || StringUtils.isEmpty(user.getPassword()))
            return null;
        Map<String,String> map = new HashMap<String, String>();
        map.put("userName",user.getUserName());
        map.put("password",BGUtils.MD5(user.getPassword()));
        List<User> byMap = repository.getByMap(map);
        if(byMap.size()>0)return byMap.get(0);
        return null;
    }


    /**
     * 判断用户是否存在
     * @param user
     * @return
     */
    public boolean isExists(User user) {
        Map<String,String> map = new HashMap<String, String>();
        if(!StringUtils.isEmpty(user.getUserName()))
            map.put("userName",user.getUserName());
        if(!StringUtils.isEmpty(user.getEmail()))
            map.put("email",user.getEmail());
        List<User> byMap = repository.getByMap(map);
        return byMap.size()>0;
    }

}
